﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kugar.Core.BaseStruct;

namespace Kugar.Core.Data
{
    public static class DatabaseHelperFactory
    {
        /// <summary>
        ///     使用连接字符串创建一个连接类(无连接池)
        /// </summary>
        /// <param name="dbType"></param>
        /// <param name="connectString"></param>
        /// <returns></returns>
        public static IKugarConnection Create(DatabaseType dbType, string connectString)
        {
            switch (dbType)
            {
                case DatabaseType.MsSql:
#if SQL
                    return new MSSQLHelper.SQLDataHelper(connectString);
#else
                    throw new NotImplementedException("当前未编译MSSQL类库,请在编译符号中加入 SQL ");
#endif
                case DatabaseType.Sqlite:
#if SQLite
                    return new SqliteHelper.SqliteHelper(connectString);
#else
                    throw new NotImplementedException("当前未编译SQLite类库,请在编译符号中加入 SQLite ");
#endif
                default:
                    throw new NotImplementedException();
            }
        }

        /// <summary>
        ///     从指定的连接对象中创建Helper类,会根据传入的连接类的类型进行自动判断
        /// </summary>
        /// <param name="conn">原始的连接对象</param>
        /// <returns></returns>
        public static IKugarConnection Create(System.Data.Common.DbConnection conn)
        { 
#if SQL
            if (conn is System.Data.SqlClient.SqlConnection)
            {
                return new MSSQLHelper.SQLDataHelper(conn);
            }
#endif

#if SQLite
            if (conn is System.Data.SQLite.SQLiteConnection)
            {
                return new SqliteHelper.SqliteHelper(conn);
            }
#endif

            throw new NotImplementedException();
        }

        public static IKugarConnection CreateFromConfig(DatabaseType dbType, string key)
        {
            //var config = Configuration.ConfigManage.GetCurrentConfigManage();

            var config = Configuration.CustomConfigManager.Default.ConnectionSettings;

            var connectItem = config[key];

            return Create(dbType, connectItem.Value.ToString());
        }

        /// <summary>
        ///     使用连接字符串,创建一个指定类型池
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="connectString">连接字符串</param>
        /// <param name="maxLength">连接池的最大连接数,默认为100</param>
        /// <param name="minLength">连接池的最小连接数 默认为5</param>
        /// <returns></returns>
        public static RecyclablePool<IKugarConnection> CreatePool(DatabaseType dbType,string connectString,int maxLength=100,int minLength=5)
        {
            switch (dbType)
            {
                case DatabaseType.MsSql:
#if SQL
                    return new MSSQLHelper.SQLHelperPool(connectString, maxLength, minLength);
#else
                    throw new NotImplementedException("当前未编译MSSQL类库,请在编译符号中加入 SQL ");
#endif                   
                case DatabaseType.Sqlite:
#if SQLite
                    return new SqliteHelper.SQLiteHelperPool(connectString, maxLength, minLength);
#else
                    throw new NotImplementedException("当前未编译SQLite类库,请在编译符号中加入 SQLite ");
#endif
                default:
                    throw new NotImplementedException();
            }

            return null;
        }

        /// <summary>
        ///     使用web.config或app.config的ConnectionSetting中指定关键字对应的连接创建连接池
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="key"></param>
        /// <param name="maxLength">连接池的最大连接数,默认为100</param>
        /// <param name="minLength">连接池的最小连接数 默认为5</param>
        /// <returns></returns>
        public static RecyclablePool<IKugarConnection> CreatePoolFromConfig(DatabaseType dbType, string key, int maxLength = 100, int minLength = 5)
        {
            //var config = Configuration.ConfigManage.GetCurrentConfigManage();

            //var connectStr = config.ConnectSetting[key];

            //return CreatePool(dbType, connectStr, maxLength, minLength);



            var config = Configuration.CustomConfigManager.Default.ConnectionSettings;

            var connectItem = config[key];

            return CreatePool(dbType, connectItem.Value.ToString());

        }
    }

    /// <summary>
    ///     数据库类型
    /// </summary>
    public enum DatabaseType
    {
        MsSql,
        Sqlite,
        Access
    }
}
